package cmo.neutech.day14;

import java.io.*;
import java.util.*;

public class StudentSystem {

    static Map<String ,Student> studentMap = new LinkedHashMap<>();
    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        load();
        while (true) {
            System.out.println("1.查看所有学生信息");
            System.out.println("2.输入姓名搜索学生信息");
            System.out.println("3.增加学生信息");
            System.out.println("4.修改学生信息");
            System.out.println("5.删除学生信息");
            System.out.println("6.退出");
            System.out.println("请输入编号选择功能");
            int index = input.nextInt();
            if (index == 1) {
                selectAll();
            } else if (index == 2) {
                selectByName();
            } else if (index == 3) {
                add();
            } else if (index == 4) {
                revise();
            } else if (index == 5) {
                delete();
            } else if (index == 6) {
                break;
            } else {
                System.err.println("没有此功能");
            }
        }
    }

    /**
     *
     */
    public static void selectAll() {
        // 输出所有学生信息
        if(studentMap.size() == 0){
            System.out.println("没有学生信息");
        }else {
            Collection<Student> values = studentMap.values();
            for (Student value : values) {
                System.out.println(value.toString());
            }
        }
    }

    public static void selectByName() {
        System.out.println("请输入要查询的学生姓名");
        String name = input.next();
        Student student = studentMap.get(name);
        if(name == null){
            System.out.println("没有此学生");
        }else{
            System.out.println(student.toString());
        }
    }

    public static void add() {
            Student student = new Student();

            System.out.println("请输入学号");
            student.setNo(input.nextInt());

            System.out.println("请输入姓名");
            student.setName(input.next());

            System.out.println("请输入年龄");
            student.setAge(input.nextInt());

            System.out.println("请输入身高");
            student.setHeight(input.nextDouble());

            System.out.println("请输入性别");
            student.setSex(input.next());

            studentMap.put(student.getName(),student);

            save();
    }

    public static void revise(){
        Student student = new Student();
        System.out.println("请输入要修改的学生姓名");
        student.setName(input.next());
        if(studentMap.get(student.getName()) == null){
            System.out.println("没有此学生");
        }else {
            System.out.println("请输入学号");
            student.setNo(input.nextInt());

            System.out.println("请输入年龄");
            student.setAge(input.nextInt());

            System.out.println("请输入身高");
            student.setHeight(input.nextDouble());

            System.out.println("请输入性别");
            student.setSex(input.next());

            studentMap.put(student.getName(),student);

            save();
        }
    }
    public static void delete(){
        Student student = new Student();
        System.out.println("请输入要删除的学生姓名");
        student.setName(input.next());
        if(studentMap.get(student.getName()) == null){
            System.out.println("没有此学生");
        }else {
            studentMap.remove(student.getName());
        }

        save();
    }
    public static void load() {
        InputStream input = null;
        try {
            input = new FileInputStream("C:\\Users\\ROG\\Desktop\\东软作业\\Work.26(2024.4.17)\\studentMap.txt");
            byte[] bytes = new byte[input.available()];
           input.read(bytes);
           String studentString = new String(bytes , "utf-8");
           String[] sdf = studentString.split("\n");
            for (String s : sdf) {
                 String[] studentFields =  s.split(",");
                 studentMap.put(studentFields[1],new Student(Integer.parseInt(studentFields[0]),studentFields[1],Integer.parseInt(studentFields[2]),Double.parseDouble(studentFields[3]),studentFields[4]));
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }   finally {
            if(input != null){
                try{
                    input.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }


    public static void save(){
        try {
            OutputStream add = new FileOutputStream("C:\\Users\\ROG\\Desktop\\东软作业\\Work.26(2024.4.17)\\studentMap.txt");
            Collection<Student> values = studentMap.values();
            for (Student value : values) {
                add.write(value.format().getBytes("utf-8"));
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
